package org.gikolet.ch2.android.dao;

import java.util.Date;

import org.gikolet.ch2.data.Board;
import org.gikolet.ch2.data.ThreadHeader;
import org.gikolet.util.ArgumentChecker;

import android.content.ContentValues;
import android.database.Cursor;

public final class Schema {

	public static class Boards {
		public static final String TBL_NAME = "boards";

		public enum Columns {
			ID(android.provider.BaseColumns._ID), HOST_NAME("hostName"), PATH(
					"path"), NAME("name");

			private String column;

			private Columns(String colum) {
				this.column = colum;
			}

			public String column() {
				return this.column;
			}

			@Override
			public String toString() {
				return super.toString();
			}
		}

		public static ContentValues convert(Board board) {
			ContentValues value = new ContentValues();

			value.put(Columns.ID.column, board.getId());
			value.put(Columns.HOST_NAME.column(), board.getHostName());
			value.put(Columns.PATH.column(), board.getPath());
			value.put(Columns.NAME.column(), board.getName());

			return value;
		}

		public static Board convert(Cursor c) {
			ArgumentChecker.checkNull(c);

			int idIndex = c.getColumnIndex(Columns.ID.column());
			int hostNameIndex = c.getColumnIndex(Columns.HOST_NAME.column());
			int pathIndex = c.getColumnIndex(Columns.PATH.column());
			int nameIndex = c.getColumnIndex(Columns.NAME.column());

			ArgumentChecker.checkRange(idIndex, 0, Integer.MAX_VALUE);
			ArgumentChecker.checkRange(hostNameIndex, 0, Integer.MAX_VALUE);
			ArgumentChecker.checkRange(pathIndex, 0, Integer.MAX_VALUE);
			ArgumentChecker.checkRange(nameIndex, 0, Integer.MAX_VALUE);

			Long id = c.getLong(idIndex);
			String hostName = c.getString(hostNameIndex);
			String path = c.getString(pathIndex);
			String name = c.getString(nameIndex);

			return new Board(id, hostName, path, name);
		}
	}

	public static class ThreadHeaders {
		public static final String TBL_NAME = "thread_headers";

		public enum Columns {
			ID(android.provider.BaseColumns._ID), BOARD_ID("board_id"), THREAD_KEY(
					"thread_key"), SUBJECT("subject"), RES_COUNT("res_count"), DATE(
					"date"), LAST_MODIFIED("last_modified"), BYTE_SIZE(
					"dat_byte_size");

			private String column;

			private Columns(String colum) {
				this.column = colum;
			}

			public String column() {
				return this.column;
			}
		}

		public static ContentValues convert(ThreadHeader threadHeader) {
			ContentValues value = new ContentValues();

			value.put(Columns.ID.column(), threadHeader.getId());
			value.put(Columns.BOARD_ID.column(), threadHeader.getBoard()
					.getId());
			value.put(Columns.THREAD_KEY.column(), threadHeader.getThreadKey());
			value.put(Columns.SUBJECT.column(), threadHeader.getSubject());
			value.put(Columns.RES_COUNT.column(), threadHeader.getResCount());
			value.put(Columns.DATE.column(), threadHeader.getDate().getTime());
			value.put(Columns.LAST_MODIFIED.column(), threadHeader
					.getLastModified().getTime());
			value.put(Columns.BYTE_SIZE.column(), threadHeader.getByteSize());

			return value;
		}

		public static ThreadHeader convert(Cursor c) {
			ArgumentChecker.checkNull(c);

			int idIndex = c.getColumnIndex(Columns.ID.column());

			int boardIdIndex = c.getColumnIndex(Boards.Columns.ID.column());
			int boardHostNameIndex = c.getColumnIndex(Boards.Columns.HOST_NAME
					.column());
			int boardPathIndex = c.getColumnIndex(Boards.Columns.PATH.column());
			int boardNameIndex = c.getColumnIndex(Boards.Columns.NAME.column());

			int threadKeyIndex = c.getColumnIndex(Columns.THREAD_KEY.column());
			int subjectIndex = c.getColumnIndex(Columns.SUBJECT.column());
			int resCountIndex = c.getColumnIndex(Columns.RES_COUNT.column());
			int dateIndex = c.getColumnIndex(Columns.DATE.column());
			int lastModifiedIndex = c.getColumnIndex(Columns.LAST_MODIFIED
					.column());
			int byteSizeIndex = c.getColumnIndex(Columns.BYTE_SIZE.column());

			ArgumentChecker.checkRange(idIndex, 0, Integer.MAX_VALUE);
			ArgumentChecker.checkRange(boardIdIndex, 0, Integer.MAX_VALUE);
			ArgumentChecker.checkRange(lastModifiedIndex, 0, Integer.MAX_VALUE);
			ArgumentChecker.checkRange(byteSizeIndex, 0, Integer.MAX_VALUE);

			long id = c.getLong(idIndex);

			Long boardId = c.getLong(boardIdIndex);
			String boardHostName = c.getString(boardHostNameIndex);
			String boardPath = c.getString(boardPathIndex);
			String boardName = c.getString(boardNameIndex);

			String threadKey = c.getString(threadKeyIndex);
			String subject = c.getString(subjectIndex);
			int resCount = c.getInt(resCountIndex);
			Date date = new Date(c.getLong(dateIndex));

			Date lastModified = new Date(c.getLong(lastModifiedIndex));
			Long datByteSize = c.getLong(byteSizeIndex);

			Board board = new Board(boardId, boardHostName, boardPath,
					boardName);

			return new ThreadHeader(id, board, threadKey, subject, resCount,
					date, lastModified, datByteSize);
		}
	}
}
